﻿#pragma execution_character_set("utf-8")
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_PI_2
#define M_PI_2 3.14159265358979323846/2.0
#endif
#include <stdexcept>
#include "TheTask.h"
#include "gdal_priv.h"
#include <vector>
using std::vector;
using std::runtime_error;

#define THETASK_NODATA -9999

TheTask::TheTask(QgsRasterLayer* rasLayer, const string& outname, QgsVectorLayer* maskLayerNullForNoUse, const string& gdalwarp)
{
	_outfilename = outname;
	_rasLayer = rasLayer;
	_maskLayer = maskLayerNullForNoUse;
	_gdalwarp = gdalwarp;
}
void TheTask::run()
{
	try {
		if (_rasLayer == 0) {
			throw runtime_error("输入图层指针为空。");
		}
		
		this->notifyProgress(0, 100);
		ARSSLayerDataWriter writer;
		writer.setDelegate(this);
		string error1;
		bool isOk = writer.writeRaster(_outfilename, _rasLayer, Qgis::DataType::Byte, _maskLayer, _gdalwarp, error1);
		if (isOk == false) {
			throw std::runtime_error(error1);
		}
		this->notifyDone();
		emit onSaveTaskDone();
	}
	catch (runtime_error& ex) {
		string error = ex.what() ;
		this->notifyError(error);
	}
}

void TheTask::updatePercent(int percent)
{
	this->notifyProgress(percent, 100);
}